

import generate_receipt, store_credit
from Tkinter import *


class main():
    def __init__(self,subTotal,taxTotal,ovrTotal,current_sale, receipt, root, parent):
        self.root = root
        self.subTotal = subTotal
        self.taxTotal = taxTotal
        self.ovrTotal = ovrTotal
        self.current_sale = current_sale
        self.receipt = receipt
        self.remBalance = StringVar()        
        self.creditLedger = store_credit.main(parent)
        self.tendered = StringVar()
        self.alltendered = 0.00
        self.change = StringVar()
        self.pmtType = StringVar()
        self.status = StringVar()
        self.due = StringVar()
        self.accountNum = StringVar()
        self.remBalance.set(self.ovrTotal)
        self.due.set(' Amount Due : $%.2f' % float(self.remBalance.get()))
        self.status.set(' Change Due : $ 0.00 ' )
       
        cashEntry = Entry(self.root, textvariable = self.tendered, width = 12)
        cashEntry.grid(row=0,column=0)
        cashEntry.bind('<Return>',self.cash)
        cashEntry.bind('<KP_Enter>',self.cash)
        cashButton = Button(self.root, text= 'Cash',width=12,command=self.cash).grid(row=0,column=1)
        creditButton = Button(self.root, text = 'Credit', width = 12, command = self.credit).grid(row=2,column=1)
        
        storeCreditEntry = Entry(self.root, textvariable = self.accountNum, width = 12)
        storeCreditEntry.grid(row=1,column=0)
        storeCreditEntry.bind('<Return>',self.storeCredit)
        storeCreditEntry.bind('<KP_Enter>',self.storeCredit)
        storeCreditBtn = Button(self.root, text='Store Credit',width=12,command=self.storeCredit).grid(row=1,column=1)
        
        dueLabel = Label(self.root, textvariable = self.due, width = 24)
        dueLabel.grid(row=3,column=0,columnspan=2)
        
        changeLabel = Label(self.root, textvariable = self.status, width = 24)
        changeLabel.grid(row=4,column=0,columnspan=2)
        
        doneButton = Button(self.root, text = 'Done', width = 12, command = self.finalize)
        doneButton.grid(row=5,column=1)
        
        
    def cash(self,event=None):
        tendered = float(self.tendered.get())
        self.alltendered+=tendered
        remBalance = float(self.remBalance.get())
        if tendered > remBalance:
            change = tendered - remBalance
            self.status.set(' Change Due : $ %.2f ' % change)
            self.pmtType.set('CASH')
            self.due.set(' Amount Due : $0.00' )
            self.change.set('%.2f' % change)
        else:
            self.remBalance.set('%.2f' % (remBalance-tendered))
            self.due.set(' Amount Due : $%.2f' % float(self.remBalance.get()))
            self.status.set('Applied $%.2f CASH' % tendered) 
            self.pmtType.set(self.pmtType.get()+'$%.2f CASH ' % tendered)
            self.change.set('0.00')
        
    def credit(self):
        self.tendered.set(self.remBalance.get())
        self.alltendered+=float(self.tendered.get())
        self.status.set(' Credit : Change Due $ 0.00 ')
        self.pmtType.set(self.pmtType.get()+'$%s CREDIT ' % self.tendered.get())
        self.change.set('0.00')
        self.finalize()
        
    def storeCredit(self,event=None):
        account_num = self.accountNum.get()
        if account_num == '':
            self.status.set('Need account number for store credit')
        else:
            remBalance = float(self.remBalance.get())          
            totCredit = self.creditLedger.debit_account(account_num,remBalance)
            if totCredit != 0: 
                self.remBalance.set('%.2f' % (remBalance-totCredit) )
                self.due.set(' Amount Due : $%.2f' % float(self.remBalance.get()))
                self.alltendered+=totCredit
                self.status.set('Applied %.2f Store Credit' % totCredit)
                self.current_sale.append(['0','0',None,'Store Credit','-%.2f' % totCredit])
            else:
                self.status.set('Account not found/0 Balance')

    def destroy(self):
        self.root.destroy()
        

    def finalize(self):
        if float(self.remBalance.get()) == 0:
            print self.subTotal, self.taxTotal, self.ovrTotal, self.alltendered,self.change.get(), self.current_sale
            generate_receipt.start(self.subTotal,self.taxTotal,self.ovrTotal,self.current_sale, self.receipt, '%.2f' % self.alltendered, self.change.get(), self.pmtType.get())
            self.destroy()
        else:
            self.status.set('Balance not paid')



def start(subTotal,taxTotal,ovrTotal,current_sale, receipt, root, parent):

    ins = main(subTotal,taxTotal,ovrTotal,current_sale, receipt, root)

    
    

